package com.ruoyi.iot.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.iotdb.isession.pool.SessionDataSetWrapper;
import org.apache.iotdb.session.pool.SessionPool;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ruoyi.iot.domain.Device;
import com.ruoyi.iot.domain.DeviceData;
import com.ruoyi.iot.domain.emun.ChannelConstant;
import com.ruoyi.iot.mapper.DeviceMapper;
import com.ruoyi.iot.service.IDeviceDataService;
import com.ruoyi.iotcommon.domain.DeviceDataMessage;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * 从iotdb中获取设备数据
 */
@Slf4j
@Service
public class DeviceDataServiceImpl implements IDeviceDataService{


    @Resource
    SessionPool sessionPool;

    @Resource
    DeviceMapper deviceMapper;

    @Override
    public Map<String, Object> lastDeviceData(DeviceData deviceData) {

        Device device = deviceMapper.selectDeviceByKey(deviceData.getProductKey(),deviceData.getDeviceKey());
        //sessionPool.executeLastDataQueryForOneDevice(id, id, null, false);
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'lastDeviceData'");
    }

    
    @Override
    public List<Map<String, Object>> lastDeviceDataList(DeviceData deviceData, Integer num) {

        if (num == null || num <= 0 || num > 2000) {
            num = 50;
        }
        if(!StringUtils.hasText(deviceData.getChannelKey())){
            deviceData.setChannelKey(ChannelConstant.DEFAULT_CHANNEL_KEY);
        }

        
        String tableName = "root.".concat(deviceData.getProductKey()).concat(".").concat(deviceData.getChannelKey()).concat(".").concat(deviceData.getDeviceKey());
        StringBuffer sql = new StringBuffer();
        sql.append("select ");
    
        if(StringUtils.hasText(deviceData.getPropertyKey())){
            sql.append(deviceData.getPropertyKey());
            sql.append(" ");
        }else{
            sql.append("* ");
        }

        sql.append("from ");
        sql.append(tableName);
        sql.append("limit ");
        sql.append(num);
        try {
            sessionPool.executeLastDataQuery(null);
        } catch (Exception e) {
            // TODO: handle exception
        }
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'deviceDataList'");
    }

    @Override
    public void deviceDataPage(PageRequest request, DeviceData deviceData, Long startTime, Long endTime) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'deviceDataPage'");
    }

    @Override
    public List<Map<String, Object>> deviceDataList(DeviceData deviceData, Long startTime, Long endTime) {
        try {
           SessionDataSetWrapper wrapper = sessionPool.executeQueryStatement("select * from root.tsdb.device where device_id = '");
           while (wrapper.hasNext()) {
              log.info("设备数据：{}",wrapper.next());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        throw new UnsupportedOperationException("Unimplemented method 'deviceDataList'");
    }


    @Override
    public void insertDeviceData(DeviceDataMessage deviceData) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'insertDeviceData'");
    }



    
}
